#include "uclientserver.h"
#include <QUdpSocket>
#include <QCoreApplication>

UClientServer::UClientServer(QObject *parent)
	: QObject(parent)
    , m_pSocket(new QUdpSocket(this))
{
    readConfig();//读取配置文件
    connect(m_pSocket, &QUdpSocket::readyRead, this, &UClientServer::readResponse); //获取服务器发送的信息
}

void UClientServer::readResponse()
{
    QByteArray datagram;
    datagram.resize(int(m_pSocket->pendingDatagramSize()));
    QHostAddress ip;
    quint16 port;
    m_pSocket->readDatagram(datagram.data(), datagram.size(), &ip, &port);

    emit alreadyRead(getOperatorType(datagram), datagram);
}

OPERATOR UClientServer::getOperatorType(const QByteArray& byteArray) //获取当前字段的操作类型，转为OP return
{
    OPERATOR Type;
    QDataStream ds(byteArray);
    ds >> Type;
    return Type;
}

UClientServer::~UClientServer()
{}

void UClientServer::sendMessage(const QString& text)
{
    m_pSocket->writeDatagram(serialize(OPERATOR::SAMEINFO, text), QHostAddress(serverAddress), serverPort);
}
void UClientServer::getDefaultJson()
{
    m_pSocket->writeDatagram(serialize(OPERATOR::JSONINFO), QHostAddress(serverAddress), serverPort); //向服务器发送 -获取json文件- 的消息
}
void UClientServer::updateJson(const QByteArray& data)
{
    m_pSocket->writeDatagram(serialize(OPERATOR::UPDATEINFO,data), QHostAddress(serverAddress), serverPort);
}

void UClientServer::addBook(const QString& title, const QString& author, const QString& press, const QString& isbn, const QString& date, const QString& image)
{
    Json::Value root;
    root["title"] = title.toStdString();
    root["author"] = author.toStdString();
    root["press"] = press.toStdString();
    root["isbn"] = isbn.toStdString();
    root["date"] = date.toStdString();
    root["image"] = image.toStdString();

    Json::StreamWriterBuilder writer;
    writer["indentation"] = "  ";
    // 将 JSON 对象序列化为字符串
    std::string jsonString = Json::writeString(writer, root);
    m_pSocket->writeDatagram(serialize(OPERATOR::ADDBOOK, QString::fromStdString(jsonString)), QHostAddress(serverAddress), serverPort);
}

void UClientServer::reviseBook(const QString& title, const QString& author, const QString& press, const QString& isbn, const QString& date, const QString& initisbn)
{
    Json::Value root;
    root["title"] = title.toStdString();
    root["author"] = author.toStdString();
    root["press"] = press.toStdString();
    root["isbn"] = isbn.toStdString();
    root["date"] = date.toStdString();
    root["initisbn"] = initisbn.toStdString();
    Json::StreamWriterBuilder writer;
    writer["indentation"] = "  "; // 设置缩进为两个空格 

    // 将 JSON 对象序列化为字符串  
    std::string jsonString = Json::writeString(writer, root);
    m_pSocket->writeDatagram(serialize(OPERATOR::REVISEBOOK, QString::fromStdString(jsonString)), QHostAddress(serverAddress), serverPort);
}

void UClientServer::queryBook(const QString& title, const QString& isbn, const QString& author)
{
    Json::Value root;
    root["title"] = title.toStdString();
    root["isbn"] = isbn.toStdString();
    root["author"] = author.toStdString();
    Json::StreamWriterBuilder writer;
    writer["indentation"] = "  "; // 设置缩进为两个空格 

    // 将 JSON 对象序列化为字符串  
    std::string jsonString = Json::writeString(writer, root);
    m_pSocket->writeDatagram(serialize(OPERATOR::QUERYBOOK, QString::fromStdString(jsonString)), QHostAddress(serverAddress), serverPort);
}

void UClientServer::deleteBook(const QString& isbn)
{
    Json::Value root;
    root["isbn"] = isbn.toStdString();
    Json::StreamWriterBuilder writer;
    writer["indentation"] = "  "; // 设置缩进为两个空格 

    std::string jsonString = Json::writeString(writer, root);
    m_pSocket->writeDatagram(serialize(OPERATOR::DELETEBOOK, QString::fromStdString(jsonString)), QHostAddress(serverAddress), serverPort);
}

void UClientServer::readConfig()
{
    QString iniFilePath = QCoreApplication::applicationDirPath() + "/UDP-Communication.ini";
    QSettings settings(iniFilePath, QSettings::IniFormat);
    serverAddress = settings.value("Network/serverAddress").toString();
    serverPort = settings.value("Network/serverPort").toInt();
}

UClientServer* UClientServer::Instance()
{
    static UClientServer instance;
    return &instance;
}